/**Nurse presenter**/
// $codepro.audit.disable declareAsInterface, handleNumericParsingErrors, largeNumberOfParameters, variableUsage
package edu.gatech.presenters;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JOptionPane;

import edu.gatech.datastorage.Gender;
import edu.gatech.datastorage.PatientInformation;
import edu.gatech.datastorage.TreatmentRecord;
import edu.gatech.gui.EditPatientPanel;
import edu.gatech.gui.TreatmentRecordPanel;
import edu.gatech.userclasses.Nurse;
import edu.gatech.userclasses.Patient;
import edu.gatech.utilities.Persistence;

/** Interacts with nurse GUI to 
 * perform various actions.
 * @author Tyler Natoli
 * @version 1.0
 */
public class NursePresenter {
	
	/**Nurse that logged in**/
	private final Nurse currentNurse;
	
	/**Logger*/
	private static final Logger MYLOGGER = Logger.getLogger("My logger");
	
	/**Presenter that sets the nurse that logged in
	 * 
	 * @param currentNurse
	 */
	public NursePresenter(Nurse currentNurse){
		this.currentNurse = currentNurse;
	}
	
	/** View appointments set by a patient
	 * 
	 * @param patientName
	 */
	public void viewAppointment(String patientName){
		final Patient patient = currentNurse.searchForPatient(patientName);
		if(patient != null){
			final ArrayList<String> appointments = 
				(ArrayList<String>) patient.viewAppointments();
			String listOfAppointments = "";
			for(int i = 0; i < appointments.size(); i++){
				listOfAppointments = listOfAppointments + appointments.get(i) + "\n"; // $codepro.audit.disable stringConcatenationInLoop
			}
			JOptionPane.showMessageDialog(null, listOfAppointments);
		}
		else{
			JOptionPane.showMessageDialog(null, currentNurse.searchForAppointments());
		}
	}
	
	/** Creates a treatment record
	 * 
	 * @param patientName
	 */
	public void createTreatmentRecord(String patientName){
		final Patient patient = currentNurse.searchForPatient(patientName);
		if(patient != null){
			final TreatmentRecord record = patient.getInfo().getLastRecord();
			if(record != null){
				final TreatmentRecordPanel panel = 
					new TreatmentRecordPanel(patient, currentNurse); // $codepro.audit.disable staticInstantiation
			}
			else{
				JOptionPane.showMessageDialog(null, "Patient has no treatment records");
			}
		}
		else{
			JOptionPane.showMessageDialog(null, "Patient not found");
		}
	}
	
	/**Searches for a patient
	 * 
	 * @param patientName
	 */
	public void search(String patientName){
		final Patient patient = currentNurse.searchForPatient(patientName);
		if(patient != null){
			final EditPatientPanel editPanel = 
				new EditPatientPanel(patient, currentNurse);
		}
		else{
			JOptionPane.showMessageDialog(null, "Patient not found");
		}
	}
	
	/**Creates a new patient
	 * 
	 * @param patientName
	 * @param medication
	 * @param allergy
	 * @param password
	 * @param age
	 * @param gender
	 * @param phone
	 * @param address
	 * @param pharm
	 * @param insurance
	 * @param dob
	 */
	public void createPatient(String patientName, String medication, 
			String allergy, String password, String age,
			Gender gender, String phone, String address, String pharm, 
			String insurance, String dob){
		final Patient patient = currentNurse.searchForPatient(patientName);
		if(patient == null){
			if(isInteger(age)){
				final String[] meds = medication.split(", ");
				final ArrayList<String> medications = new ArrayList<String>();
				for(int i = 0; i < meds.length; i++){
					medications.add(meds[i]);
				}
				final String[] alls = allergy.split(", ");
				final ArrayList<String> allergies = new ArrayList<String>();
				for(int j = 0; j < meds.length; j++){
					allergies.add(alls[j]);
				}
				final PatientInformation info = new 
				PatientInformation(patientName, password, Integer.parseInt(age), 
						gender, phone, address, pharm, insurance, 
						medications, allergies, dob);
				currentNurse.addPatient(info);
				Persistence.save();
				JOptionPane.showMessageDialog(null, "Patient created");
			}
			else{
				JOptionPane.showMessageDialog(null, "Invalid age entered");
			}
		}
		else{
			JOptionPane.showMessageDialog(null, "Patient already in the system");
		}
	}
	
	/**Checks if a valid age is given
	 * 
	 * @param input
	 * @return boolean if the String can be an int
	 */
	public boolean isInteger(String input){
		try{
			Integer.parseInt(input); // $codepro.audit.disable com.instantiations.assist.eclipse.analysis.unusedReturnValue
			return true;
		}
		catch(NumberFormatException e){
			MYLOGGER.log(Level.SEVERE, "Age not an int", e);
			return false;
		}
	}
	
	/**
	 * Overrides toString
	 * @return String
	 */
	public String toString(){
		return "Nurse presenter";
	}
}
